package com.lancoo.answer.widget.span;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.text.Layout;
import android.text.Selection;
import android.text.Spannable;
import android.text.method.LinkMovementMethod;
import android.text.style.ReplacementSpan;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.TextView;

import com.lancoo.answer.widget.span.view.MatchFillCallBack;


/**
 * 自定义的Span,用来绘制重要知识点标记
 */
public class MatchAnwerSpan extends ReplacementSpan implements MatchFillCallBack {

    private static final String TAG = "MatchAnwerSpan";
    private final Paint mPaint;//绘制重要知识点文本
    private final Path path;
    private final Paint paint_underline;//绘制重要知识点文本的下划线
    private int txtColor=0xff282828;
    private final int dp5;
    private final int dp4;
    private final int dp8;
    private boolean isStartFill,isEndFill,isUnderLine;
    private final int redColor=0xffff4c4c;
    private final int greenColor=0xff00c063;
    private final int blueColor=0xff0099ff;
    private final int bgColor=0xffccebff;
    private final int bgTranspationColor=0x00000000;
    private int answerPointIndex;
    private MatchFillCallBack callBack;


    public MatchAnwerSpan(Context context, Paint paint) {
        this.mPaint=paint;
        path = new Path();
        paint_underline=new Paint();
        paint_underline.setAntiAlias(true);
        paint_underline.setStrokeWidth(2.0f);
        paint_underline.setStyle(Paint.Style.STROKE);
        paint_underline.setColor(paint.getColor());
        dp5 =dip2px(context,0);
        dp4=dip2px(context,4);
        dp8=dip2px(context,8);
    }

    public int id = 0;//回调中的对应Span的ID
    private int mWidth = 0;//最长单词的宽度
    public String mText;//保存的String


    @Override
    public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
        //将返回相对于Paint画笔的文本 50== 左右两边增加的空余长度
        if (mWidth == 0)mWidth = (int) paint.measureText(mText, 0, mText.length());
        return mWidth+(isStartFill? dp5 +dp8:0)+(isEndFill? dp5 +dp8:0);
    }


    @Override
    public void draw(Canvas canvas, CharSequence text, int start, int end,
                     float x, int top, int y, int bottom, Paint paint) {
        if(!isUnderLine){
            paint.setColor(bgTranspationColor);
            Log.e(TAG, "draw: trans"+answerPointIndex );
        }else{
            paint.setColor(bgColor);
            Log.e(TAG, "draw: color"+answerPointIndex );
        }
        canvas.drawRoundRect(new RectF(x+(isStartFill? dp5 :0), top, x +
                mWidth+(isStartFill?dp8+dp5:0)+(isEndFill?dp8:0),
                y+ dp4), isStartFill?8:0, isEndFill?8:0, paint);
        //填入对应单词

        mPaint.setColor(txtColor);
        canvas.drawText(mText,0,mText.length(),x+(isStartFill? dp5 +dp8:0),(float)(y),mPaint);
        mPaint.setColor(0xff333333);
        path.moveTo(x,y+ dp4);
        path.lineTo( x + mWidth+(isStartFill? dp5 +dp8:0)+(isEndFill? dp5 +dp8:0),y+ dp4);
        canvas.drawPath(path,paint_underline);

    }

    private int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public void setmText(String mText) {
        this.mText =""+ mText+"";
    }

    public void setDrawableLineType(int drawableLineType){
        //0-作答样式,1-不合格样式,2-合格样式,3-下划线样式
        isUnderLine=false;
        switch (drawableLineType){
            case 1:
                txtColor =redColor;
                break;
            case 2:
                txtColor =greenColor;
                break;
            case 3:
                isUnderLine=true;
                break;
            case 4:
                isUnderLine=true;
                txtColor=blueColor;
                break;
            default:
                txtColor =mPaint.getColor();

        }

    }

    public void setStartFill(boolean startFill) {
        isStartFill = startFill;
    }

    public void setEndFill(boolean endFill) {
        isEndFill = endFill;
    }

    public void setUnderLine(boolean underLine) {
        isUnderLine = underLine;


    }

    public int getAnswerPointIndex() {
        return answerPointIndex;
    }

    public void setAnswerPointIndex(int answerPointIndex) {
        this.answerPointIndex = answerPointIndex;
    }


    //TextView触摸事件-->Span点击事件
    public static LinkMovementMethod Method = new LinkMovementMethod() {

        public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {


            int action = event.getAction();
            if (action == MotionEvent.ACTION_UP) {
                Log.e("dianji","onTouchEvent:");

                int x = (int) event.getX();
                int y = (int) event.getY();
                x -= widget.getTotalPaddingLeft();
                y -= widget.getTotalPaddingTop();
                x += widget.getScrollX();
                y += widget.getScrollY();
                Layout layout = widget.getLayout();
                int line = layout.getLineForVertical(y);
                int off = layout.getOffsetForHorizontal(line, x);
                MatchAnwerSpan[] link = buffer.getSpans(off, off, MatchAnwerSpan.class);
                if(link.length>0){
//                    Log.e("dianji","length:"+link.length);
                    MatchAnwerSpan matchAnwerSpan = link[0];
                    int answerPointIndex = matchAnwerSpan.getAnswerPointIndex();
                    matchAnwerSpan.onClick(answerPointIndex);
                    return false;
                }else{
//                    Log.e("dianji","length:"+0);
                }

            }
            return false;
        }

        @Override
        public void initialize(TextView widget, Spannable text) {
            Selection.removeSelection(text);
        }
    };

    @Override
    public void onClick(int position) {
        if(callBack!=null){
            callBack.onClick(position);
        }
    }


    public void setCallBack(MatchFillCallBack callBack) {
        this.callBack = callBack;
    }

}
